package com.Darkra1Zzz.controller;

import com.Darkra1Zzz.entity.PageInfo;
import com.Darkra1Zzz.entity.Type;
import com.Darkra1Zzz.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author xzw
 * @date 2021/7/2 2:37
 */
@Controller
public class TypeController {
    @Resource
    private TypeService typeService;

    /**
     * 通过id查询类型
     */
    @RequestMapping("findTypeById")
    public String findTypeById(Integer id, HttpSession session){
        Type type=typeService.findTypeBYId(id);
        session.setAttribute("type",type);
        return  "typeedit";
    }

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "findType")
    public String findType(Integer id,String name,String type,Integer pageIndex, Integer pageSize, Model model){

        Type type1=new Type();
        if (id!=null){
            type1.setId(id);
        }
        if (name!=null){
            type1.setName(name);
        }
        if (type!=null){
            type1.getType(type);
        }
        PageInfo<Type> pageInfo = typeService.findPageInfo(type,pageIndex,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",type1.getName());
        return "typelist";
    }
    /**
     * 修改类型信息
     */
    @RequestMapping(value = "updateType" ,method = RequestMethod.POST )
    public String updateType(Type type) {
        int s = typeService.updateType(type);
        return "redirect:findType";
    }


    /**
     * 添加类型信息
     */
    @RequestMapping(value = "addType" ,method = RequestMethod.POST)
    @ResponseBody
    public String addType(@RequestBody Type type) {
//        int不能用改为Boolean
        boolean s = typeService.addType(type);
        return "typelist";
    }


    /**
     * 删除类型信息
     */
    @RequestMapping( "deleteType")
    @ResponseBody
    public String deleteType(Integer id) {
        //        int不能用改为Boolean
        boolean s = typeService.deleteType(id);
        return "typelist";
    }


    /**
     * 导出Excel
     */
    @RequestMapping(value = "exportTypeList", method = RequestMethod.POST)
    @ResponseBody
    public List<Type> exportType(){
        List<Type> typeList = typeService.getAll();
        return typeList;
    }
}
